/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.doeacc.tms.web.bean;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.doeacc.tms.web.util.JsfUtil;

/**
 *
 * @author slok
 */
@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean {

    public String userId;
    public String password;

    public String getPassword() {
        return "";
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String orginalRequestURI(HttpServletRequest request) {
        String requestURI = (String) request.getAttribute("javax.servlet.forward.request_uri");
        if (requestURI == null || requestURI.endsWith("login")) {
            requestURI = request.getParameter("orginal_request_uri");
        }
        if (requestURI == null) {
            requestURI = request.getContextPath() + "/home";
        }
        return requestURI;
    }

    public String doLogin() {
        HttpServletRequest request = getHttpServletRequest();
        HttpServletResponse response = getHttpServletResponse();

        String orginalRequestURI = request.getParameter("orginal_request_uri");

        try {
            if (request.getUserPrincipal() != null) {
                request.logout();
            }
            request.login(userId, password);
            if (orginalRequestURI == null || "".equals(orginalRequestURI)) {
                return "pretty:home";
            } else {
                response.sendRedirect(orginalRequestURI);
            }
            return null;
        } catch (ServletException ex) {
            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("Login failed");
            return "pretty:login";
        } catch (IOException ex) {
            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("Could not rediect to the requested page");
            return "pretty:home";
        }
    }

    protected HttpServletRequest getHttpServletRequest() {
        return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    }

    protected HttpServletResponse getHttpServletResponse() {
        return (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
    }

    public String logout() {
        HttpServletRequest request = getHttpServletRequest();
        try {
            request.logout();
            return "pretty:home";
        } catch (ServletException ex) {
            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("logout failed");
            return "pretty:login";
        }
    }
}
